<?php
namespace App\Service;
use App\Entity\Money;
use App\Entity\MoneyLog;
use App\Entity\MoneyType;
use App\Entity\User;
use App\Repository\MoneyConfigRepository;
use App\Repository\MoneyLogRepository;
use App\Repository\MoneyLogTypeRepository;
use App\Repository\MoneyRepository;
use App\Repository\MoneyStaticRepository;
use App\Repository\MoneyTypeRepository;
use App\Repository\UserRepository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

class MoneyService extends ServiceEntityRepository
{

    //资金账户设置
    const __MONEY_XIAN_JIN__    = 1;
    const __MONEY_INSURE__      = 2;
    const __MONEY_JI_FEN__      = 3;

    //日志类型设置


    public $userRepository;
    public $moneyRepository;
    public $moneyLogRepository;
    public $moneyTypeRepository;
    public $moneyConfigRepository;
    public $moneyStaticRepository;
    public $moneyLogTypeRepository;

    public function __construct(
        ManagerRegistry $registry
        ,UserRepository $userRepository
        ,MoneyRepository $moneyRepository
        ,MoneyLogRepository $moneyLogRepository
        ,MoneyTypeRepository $moneyTypeRepository
        ,MoneyConfigRepository $moneyConfigRepository
        ,MoneyStaticRepository $moneyStaticRepository
        ,MoneyLogTypeRepository $moneyLogTypeRepository
    )
    {
        parent::__construct($registry, Money::class);
        $this->userRepository = $userRepository;
        $this->moneyRepository = $moneyRepository;
        $this->moneyLogRepository = $moneyLogRepository;
        $this->moneyTypeRepository = $moneyTypeRepository;
        $this->moneyConfigRepository = $moneyConfigRepository;
        $this->moneyStaticRepository = $moneyStaticRepository;
        $this->moneyLogTypeRepository = $moneyLogTypeRepository;
    }


    public function mod(User $user,$money,$moneyTypeId,$logTypeId,$tips="",$param=[])
    {//通用资金添加，增加资金的时候变动余额，增加记录，改变统计信息
        $this->_em->beginTransaction();
        try {
            $moneyType = $this->moneyTypeRepository->find($moneyTypeId);
            $logType = $this->moneyLogTypeRepository->findOneBy(array("code"=>$logTypeId));
            $userMoney = $this->moneyRepository->getUserMoneyByType($user,$moneyType);
            $curMoney = $userMoney->getMoney();
            //变更余额
            $userMoney->setMoney( $curMoney + $money);
            $this->_em->persist($userMoney);

            //变更数据统计
            $userStatic = $this->moneyStaticRepository->getUserMoneyStaticByType($user,$moneyType,$logType);
            $curStaticMoney = $userStatic->getMoney();
            $userStatic->setMoney($curStaticMoney + $money);
            $this->_em->persist($userStatic);

            //增加流水记录
            $MoneyLog = new MoneyLog();
            $MoneyLog->setMoney($money)
                ->setUser($user)
                ->setMoneyType($moneyType)
                ->setLogType($logType)
                ->setCreateAt(new \DateTime())
                ->setTips($tips)->setParam($param);
            $this->_em->persist($MoneyLog);
            $this->_em->flush();
        } catch (\Exception $exception) {
            $this->_em->rollback();
            throw $exception;
            throw new \Exception($exception->getMessage()."[".$exception->getLine()."]",500);
        }
        $this->_em->commit();
    }

    /**
     * @param User $user
     * @param $money
     * @throws \Exception
     */
    public function addInsureMoney(User $user ,$money)
    {
        return $this->mod($user,$money,self::__MONEY_INSURE__,110,"保证金充值",[]);
    }

}